<template>
  <div class="login">
    <van-nav-bar>
      <template #left>
        <i @click="goBack" class="icon iconfont iconbtn_nav_back leftArro"></i>
      </template>
    </van-nav-bar>
    <div class="content">
      <h3 class="slogan">您好,请登录</h3>
      <van-form ref="myform" class="form" @submit="login">
        <van-field
          v-model="form.mobile"
          placeholder="请输入手机号"
          name="mobile"
          :rules="rules.mobile"
        >
          <template #left-icon>
            <i class="iconfont iconbianzu1"></i>
          </template>
        </van-field>
        <van-field
          v-model="form.code"
          placeholder="请输入验证码"
          :rules="rules.code"
        >
          <template #left-icon>
            <i class="iconfont iconyanzhengma"></i>
          </template>
          <template #button>
            <span class="blue" @click="getCode"
              >{{ isback ? totaltime + 's后重试' : '获取验证码' }}
            </span>
          </template>
        </van-field>
        <p class="userPri">
          登录即同意 <span class="blue">《用户使用协议》</span> 和
          <span class="blue">《隐私协议》</span>
        </p>
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>
<script>
import { apiGetCode, toLogin } from '@/api/login'
import { setLocal } from '@/utils/local'

export default {
  data () {
    return {
      isback: false,
      totaltime: 5,
      form: {
        mobile: '18888881111',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请填写手机号', trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请正确输入手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '请填写验证码', trigger: 'onBlur' },
          {
            pattern: /^\d{4}$/,
            message: '请正确填写4位数验证码',
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    async getCode () {
      if (this.isback === true) {
        return
      }

      try {
        await this.$refs.myform.validate('mobile')
        this.isback = true
        const timeId = setInterval(() => {
          this.totaltime--
          if (this.totaltime === 0) {
            clearInterval(timeId)
            this.totaltime = 5
            this.isback = false
          }
        }, 1000)
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true,
          duration: 0
        })
        const resCode = await apiGetCode(this.form.mobile)
        console.log(resCode)

        this.$toast.success(resCode.data)
      } catch (error) {
        this.$toast.fail(error.message)
      }
    },
    // 登录
    async login () {
      this.$toast.loading({
        duration: 0
      })
      try {
        const resLogin = await toLogin(this.form)
        resLogin.data.user.avatar =
          'http://localhost:1337' + resLogin.data.user.avatar
        console.log(resLogin)
        setLocal('token', resLogin.data.jwt)
        this.$store.commit('setUserInfo', resLogin.data.user)
        this.$toast.success('登录成功')
        const direct = this.$route.query._redirect
        if (direct) {
          this.$router.push(direct)
        } else {
          this.$router.push('/my')
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 回退
    goBack () {
      const direct = this.$route.query._redirect
      if (!direct) {
        this.$router.go(-1)
      } else {
        this.$router.push('/find')
      }
    }
  }
}
</script>
<style lang="less">
.login {
  .form,
  .slogan {
    padding-left: 15px;
  }
  .van-nav-bar__left {
    padding: 0;
  }
  .leftArro {
    font-size: 44px;
  }
  h3 {
    font-size: 24px;
    margin-bottom: 30px;
  }
  .blue {
    color: #00b8d4;
  }
  .userPri {
    font-size: 12px;
    margin-bottom: 60px;
  }
  .van-field__left-icon {
    margin-right: 15px;
  }
}
</style>
